﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Friso.Entities;
using Friso.Business;
using FetCo.Data;
using FetCo.Framework;
using System.IO;
public partial class V_FactoryManagement_Building : System.Web.UI.Page
{

    #region viewstate
    protected long Id
    {
        get
        {
            object o = ViewState["Id"];
            if (o != null)
                return (long)o;
            return long.MinValue;
        }
        set
        {
            ViewState["Id"] = value;
        }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        


        if (IsPostBack)
        {
            return;
        }
        if (UserRight.GetUserPermission() != FetConstant.Admin)
        {
            Response.Redirect("~/Login.aspx");
        }
        else
        {
            string mes = Request.QueryString["alert"];
            if (!string.IsNullOrEmpty(mes))
            {
                ltrmessage.Text = mes;
            }
            loaduUserid();
            LoadData();
        }
    }
    private void loaduUserid()
    {
        Criteria cri = new Criteria();
        ddluserid.Items.Clear();
        ddluserid.Items.Add(new ListItem((string)Utilitys.Dictionary["option"], "0"));
        cri.add("[" + Friso.Entities.User._Table + "].[" + UserColumns.PermissionId + "]", FetConstant.XUATBAN, Condition.EQUAL, Condition.AND);
        UserCollection _userCollection = UserService.GetUserListByParam("[" + Friso.Entities.User._Table + "].[" + UserColumns.UserId + "]", OrderDirection.ASC, cri.Criter);
        foreach (Friso.Entities.User _User in _userCollection)
        {
            ddluserid.Items.Add(new ListItem(_User.UserName, _User.UserId.ToString()));
        }
    }
    protected void LoadData()
    {
        string strid = Request.QueryString["id"];
        if (!string.IsNullOrEmpty(strid))
        {
            this.Id = long.Parse(strid);
            Category _Category = CategoryService.GetCategory(this.Id);
            if (_Category != null)
            {
                txtName.Text = _Category.Name;
                txtCode.Text = _Category.Code;
                txtCode.Enabled = false;
                txtOrder.Text = _Category.Order.ToString();
                txtnameen.Text = _Category.NameEn;
                txtDescription.Text = _Category.Desc;
                fck_SummaryVn.Value = _Category.Summary;
                ddluserid.SelectedValue = _Category.UserId.ToString();
            }
        }
    }
    protected void btnSave_OnClick(object sender, EventArgs e)
    {
        Criteria cri = new Criteria();
        cri.add("[" + Friso.Entities.Category._Table + "].[" + CategoryColumns.Code + "]", txtCode.Text.Trim().ToUpper(), Condition.EQUAL, Condition.AND);
        CategoryCollection _CategoryCollection = CategoryService.GetCategoryListByParam("[" + Friso.Entities.Category._Table + "].[" + CategoryColumns.CategoryID + "]", OrderDirection.ASC, cri.Criter);
        if (this.Id == long.MinValue && _CategoryCollection.Count > 0)
        {
            Alert.Show("Không được trùng mã!");
            return;
        }
        Category _Category = new Category();
        if (this.Id != long.MinValue)
        {
            _Category = CategoryService.GetCategory(this.Id);
            if (_Category.Code != txtCode.Text.Trim() && _CategoryCollection.Count > 0)
            {
                Alert.Show("Không được trùng mã!");
                return;
            }
        }
        else
        {
            _Category.Order = RTISettings.DefaultOrder;
        }
        _Category.Code = txtCode.Text.Trim();
        _Category.Name = txtName.Text.Trim();
        _Category.NameEn = txtnameen.Text;
        if (!string.IsNullOrEmpty(txtOrder.Text.Trim()))
            _Category.Order = int.Parse(txtOrder.Text.Trim());
        _Category.Desc = txtDescription.Text.Trim();
        _Category.Summary = fck_SummaryVn.Value;
        _Category.UserId = long.Parse(ddluserid.SelectedValue.ToString());
        if (this.Id != long.MinValue)
        {
            CategoryService.UpdateCategory(_Category);
            Response.Redirect("CategoryList.aspx");
        }
        else
        {
            CategoryService.CreateCategory(_Category);
            this.Id = _Category.CategoryID;
            Response.Redirect("CategoryList.aspx");
        }
    }
}
